import 'package:flutter/foundation.dart';
import 'package:syria_sports/presentation/additional_operations_page/bloc/dynamic_ui_screen.dart';
import 'package:syria_sports/presentation/admin_home_tabs/other_tab/widgets/other_option_item.dart';
import 'package:syria_sports/presentation/public_home_page/bloc/public_home_screen.dart';
import 'package:syria_sports/presentation/sync_screen/sync_screen.dart';

import '../../../../core/utils/image_constant.dart';
import '../../../../core/utils/navigator_service.dart';
import '../../../../core/utils/pref_utils.dart';
import '../../../../data/remote/database_helper.dart';
import '../../../../dialogs/art_dialog.dart';
import '../../../../theme/bloc/theme_bloc.dart';
import '../../../../widgets/app_bar/appbar_subtitle.dart';
import '../../../../widgets/app_bar/custom_app_bar.dart';
import '../../../admins/admins_page/bloc/admins_screen.dart';
import '../../../coaches/all_coachs_page/bloc/coaches_tab_screen.dart';
import '../../../players/all_players_page/bloc/all_players_screen.dart';
import '../../../attendances_list_page/test/test_calander.dart';
import '../../../exercises/exercises_page/bloc/exercises_screen.dart';
import '../../../gyms_admins/gym_admins_page/bloc/admins_screen.dart';
import '../../../gyms/gyms_page/bloc/gyms_screen.dart';
import '../../../gyms/my_gyms_page/bloc/gyms_screen.dart';
import '../../../settings_page/settings_screen.dart';
import '../../../sports/sports_page/bloc/sports_screen.dart';
import '../../../subscriptions/subscriptions_page/bloc/subscriptions_screen.dart';
import '../../../tools/tools_page/bloc/tools_screen.dart';
import '../../../user_complaints_page/bloc/user_complaints_screen.dart';
import 'other_tab_cubit.dart';
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';

class OtherTabScreen extends StatelessWidget {
  const OtherTabScreen({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    double height = MediaQuery.of(context).size.height;
    double width = MediaQuery.of(context).size.width;

    return Container(
      decoration: BoxDecoration(
        image: DecorationImage(
          image: AssetImage(ImageConstant.imgBackground),
          fit: BoxFit.cover,
        ),
      ),
      child: Scaffold(
        appBar:_buildAppBar(context:context ),
        body: BlocConsumer<OtherTabCubit, OtherTabState>(
          listener: (context, state) {
            // TODO: implement listener
          },
          builder: (context, state) {
            var cubit = OtherTabCubit.get(context);
            var listWidgets = [
              OtherOptionItem(
                title: 'نوادي',
                imgPath: ImageConstant.icGyms,
                onTapped: (){
                  Navigator.push(context, MaterialPageRoute(builder: (context) => const MyGymsScreen()));
                },
              ),
              if(PrefUtils.isAdmin())
                OtherOptionItem(
                title: 'النوادي',
                imgPath: ImageConstant.icGyms,
                onTapped: (){
                  Navigator.push(context, MaterialPageRoute(builder: (context) => const GymsScreen()));
                },
              ),
              OtherOptionItem(title: 'الصفحة الرئيسة', imgPath:
              ImageConstant.icVisitors,
                onTapped: (){
                  Navigator.of(context, rootNavigator: true).pushReplacement(
                    MaterialPageRoute(
                      builder: (context) => const PublicHomeScreen(),
                    ),
                  );
                },
              ),
              OtherOptionItem(title: 'قوائم الحضور', imgPath: ImageConstant.imgAttendanceIcon,
                onTapped: (){
                  Navigator.push(context, MaterialPageRoute(builder: (context) => const TableRangeExample()));
                },
              ),
              if(PrefUtils.isGymAdmin())
                OtherOptionItem(
                  title: 'الاشتراكات',
                  imgPath: ImageConstant.icSubscriptions,
                  onTapped: (){
                    Navigator.push(context, MaterialPageRoute(builder: (context) => const SubscriptionsScreen()));
                  },
                ),
                if(PrefUtils.isAdmin())
                  OtherOptionItem(
                    title: 'التمارين',
                    imgPath: ImageConstant.icSports,
                    onTapped: (){
                      Navigator.push(context, MaterialPageRoute(builder: (context) =>const ExercisesScreen()));
                    },
                  ),
              if(PrefUtils.isAdmin())
                OtherOptionItem(
                  title: 'الرياضات',
                  imgPath: ImageConstant.icSports,
                  onTapped: (){
                    Navigator.push(context, MaterialPageRoute(builder: (context) => SportsScreen.builder(context: context)));
                  },
                ),
              if(PrefUtils.isAdmin())
                OtherOptionItem(
                  title: 'ادوات التدريب',
                  imgPath: ImageConstant.icExerciseTools,
                  onTapped: (){
                    Navigator.push(context, MaterialPageRoute(builder: (context) => ToolsScreen.builder(context: context)));
                  },
                ),
              /*OtherOptionItem(title: 'روابط التواصل', imgPath: ImageConstant.icSocialMedia,
                    onTapped: (){
                      Navigator.push(context, MaterialPageRoute(builder: (context) => const LinksScreen()));
                    },
                  ),*/
                if(PrefUtils.isAdmin())
                OtherOptionItem(title: 'المُدِيرون', imgPath: ImageConstant.icAdministrator,
                  onTapped: (){
                    Navigator.push(context, MaterialPageRoute(builder: (context) => const AdminsScreen()));
                  },
                ),
              if(PrefUtils.isAdmin())
                OtherOptionItem(title: 'اللاعبين', imgPath: ImageConstant.icAdministrator,
                  onTapped: (){
                    Navigator.push(context, MaterialPageRoute(builder: (context) => const AllPlayersScreen()));
                  },
                ),
              if(PrefUtils.isAdmin())
                OtherOptionItem(title: 'المدربين', imgPath: ImageConstant.icAdministrator,
                  onTapped: (){
                    Navigator.push(context, MaterialPageRoute(builder: (context) => const AllCoachesScreen()));
                  },
                ),
              if(PrefUtils.isAdmin())
                OtherOptionItem(title: 'مُدِيرون النوادي', imgPath: ImageConstant.icAdministrator,
                  onTapped: (){
                    Navigator.push(context, MaterialPageRoute(builder: (context) => const GymsAdminsScreen()));
                  },
                ),
                if(PrefUtils.isAdmin())
                OtherOptionItem(title: 'الشكاوى', imgPath: ImageConstant.icComplaints,
                  onTapped: (){
                    Navigator.push(context, MaterialPageRoute(builder: (context) => const UserComplaintsScreen()));
                  },
                ),
              if(!kIsWeb)
                OtherOptionItem(
                  title: 'المزامنة',
                  imgPath: ImageConstant.imgSync,
                  onTapped: (){
                    Navigator.push(NavigatorService.navigatorKey.currentContext??context, MaterialPageRoute(builder: (context) => const SyncScreen()));
                  },
                ),
                if(PrefUtils.isGymAdmin())
                OtherOptionItem(title: 'عمليات إضافية', imgPath: ImageConstant.icAdministrator,
                  onTapped: (){
                    Navigator.push(context, MaterialPageRoute(builder: (context) => const DynamicUiScreen()));
                  },
                ),
              OtherOptionItem(title: 'تسجيل خروج', imgPath: ImageConstant.icLogout,
                onTapped: ()async{

                  var res = await AlertDialogs.yesCancelDialog(context, 'تأكيد تسجيل الخروج', 'سيتم حذف جميع البيانات لمخزنة مؤقتا في هذا الجهاز');

                  if(res == DialogsAction.yes ){
                    var darkMode = PrefUtils.getThemeData();
                    PrefUtils.clearPreferencesData();
                    await DatabaseHelper.clearAllTables().then((value){
                      Navigator.of(context, rootNavigator: true).pushReplacement(
                        MaterialPageRoute(
                          builder: (context) => const PublicHomeScreen(),
                        ),
                      );
                      ThemeBloc themeCubit = BlocProvider.of(context);
                      themeCubit.add(ThemeChangeEvent(themeType:darkMode));
                    }).catchError((error){
                      Navigator.of(context, rootNavigator: true).pushReplacement(
                        MaterialPageRoute(
                          builder: (context) => const PublicHomeScreen(),
                        ),
                      );
                      ThemeBloc themeCubit = BlocProvider.of(context);
                      themeCubit.add(ThemeChangeEvent(themeType:darkMode));
                    });
                  }


                },
              ),
            ];
            return SizedBox(
              height: height,
              width: width,
              child: GridView.builder(
                gridDelegate: const SliverGridDelegateWithMaxCrossAxisExtent(
                  maxCrossAxisExtent: 220,
                ),
                itemCount: listWidgets.length,
                itemBuilder: (context,index){
                  return listWidgets[index];
                },
              ),

            );
          },
        ),
      ),
    );
  }
  PreferredSizeWidget _buildAppBar({required BuildContext context}) {
    return CustomAppBar(
      title: AppbarSubtitle(text: 'المزيد'),
      leading: IconButton(
        icon: const Icon(Icons.settings),
        onPressed: (){
          Navigator.push(context, MaterialPageRoute(builder: (context) => const SettingsScreen()));
        },
      ),
    );
  }

}
